<?php

namespace App\Http\Controllers\Admin;

use App\Repository\Orders\OrderCommissionCountRepository as model;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;
use App\Repository\UploadRepository;
use Illuminate\Support\Facades\DB;

// 分销商
class CommissionCountController extends BaseController
{
  private $repoist;
  public function __construct(model $model)
  {
    $this->repoist = $model;
  }
  public function store(Request $request)
  {
    return $this->repoist->add($request);
  }
  public function index(Request $request)
  {
    $where = [];
    if (empty($request->get('dist_id'))) {
      $where['where']['fail_id'] = $request->get('id');
    }

    if (($request->get('user')->role != 1)) {
      // $where['where']['shop_id'] = $request->get('user')->shop_id;
    }
    if (!empty($request->get('phone'))) {
      $where['where']['phone'] = $request->get('phone');
    }
    if (!empty($request->get('name'))) {
      $where['like'] = ['name' => $request->get('name')];
    }
    if (!empty($request->get('number'))) {
      $where['like'] = ['name' => $request->get('number')];
    }
    $where['whereor']['where']['sup_id'] = $request->get('user')->shop_id;
    $where['whereor']['whereor']['shop_id'] = $request->get('user')->shop_id;
    $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
    $where['limit']['number'] = $request->get('pageSize');
    // var_dump($where);
    // DB::enableQueryLog();
    $data =  $this->repoist->index($where);
    // $sql = DB::getQueryLog();
    // var_dump($sql);
    return $data;
  }
  public function save(Request $request)
  {
    if (!$request->get('id')) {
      return $this->returnMsg(1, '请传递id参数');
    }
    $where = ['id' => $request->get('id')];
    return $this->repoist->edit($where, $request);
  }
  public function  show($id)
  {
    $where = ['id' => $id];
    //查询一条数据
    $return = $this->repoist->getOne($where, ['id', 'name', 'product_id', 'goods_point', 'goods_detail', 'goods_remark', 'main_pic', 'settle_require']);
    if (!empty($return)) {
      // $return = usfirstArr($return);
      // 将文本模式 格式化
      if (!empty($return->goods_detail)) {
        $return->goods_detail = html_entity_decode($return->goods_detail);
      }
      if (!empty($return->goods_remark)) {
        $return->goods_remark = html_entity_decode($return->goods_remark);
      }
    }
    return $this->returnMsg(200, '查询成功', $return);
  }

  public function listss(Request $request)
  {
    $where = [];
    if (!empty($request->get('name'))) {
      $where['where'] = ['name' => $request->get('name')];
    }

    $return = $this->repoist->lists($where);
    return $return;
  }

  public function destroy(Request $request, $id)
  {
    if (!$id) {
      return $this->returnMsg(1, '请传递参数');
    }
    $where = [
      'id' => $id,
      'shop_id' => $request->user->shop_id
    ];
    return $this->repoist->del($where);
  }
  // 导入功能
  public function import(Request $request)
  {
    $model = new UploadRepository();
    $res = $model->upload($request, 'file');
    if (empty($res['file_src'])) {
      return [
        'code' => 100,
        'msg' => !empty($res['msg']) ? $res['msg'] : '上传失败',
        'fileName' => ''
      ];
    }
    if (!empty($request->get('updateSupport'))) {
      $request['type'] = $request->get('updateSupport');
    }
    $request['file_name'] = $res['file_name'];
    $request['upload_rout'] = $res['file_src'];
    $request['time'] = $request->get('time');
    $request['is_value'] = $request->get('is_value');
    $request['type'] = $request->get('type');

    return $this->repoist->add($request);
  }
}
